﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using DotNetNuke.Data;
using DotNetNuke.Data.PetaPoco;
using DotNetNuke.ComponentModel.DataAnnotations;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Roles;

namespace DotNetNuke.Modules.mwOrgChart.Components.Business
{
    public class mwHierarchyRelationsController
    {
        /// <summary>
        /// Gets the Hierarchy relations by Node
        /// </summary>
        /// <param name="nodeId">The Hierarchy NodeId</param>
        public static IEnumerable<mwHierarchyRelations> GetHierarchyRelationsByNode(int nodeId, bool getFullObjects = false)
        {
            IEnumerable<mwHierarchyRelations> hierarchy;
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<mwHierarchyRelations>();
                hierarchy = rep.Get().Where(x => x.HNodeID == nodeId);
            }
            return hierarchy;
        }

        public static IEnumerable<mwHierarchyRelations> GetHierarchyRelations(int portalId, int userId, bool getFullObjects = false)
        {
            IEnumerable<mwHierarchyRelations> hierarchy;
            using (IDataContext ctx = DataContext.Instance())
            {
                ctx.BeginTransaction();

                var rep = ctx.GetRepository<mwHierarchyRelations>();
                hierarchy = rep.Get();

                if (getFullObjects)
                {
                    foreach (var item in hierarchy)
                    {
                        var rcc = new RoleController();

                        item.User = UserController.GetUserById(portalId, userId);
                        item.Role = rcc.GetRole(item.RoleID, portalId);
                        item.Node = mwHierarchyRelationsController.GetHierarchyNode(item.HNodeID, portalId, true);
                    }
                }

                ctx.Commit();
            }
            return hierarchy;
        }

        /// <summary>
        /// Gets the Hierarchy relations by UserID
        /// </summary>
        /// <param name="UserId">The DNN UserId</param>
        public static IEnumerable<mwHierarchyRelations> GetHierarchyRelationsByUserID(int portalId, int userId, bool getFullObjects = false)
        {
            IEnumerable<mwHierarchyRelations> hierarchy;
            mwHierarchyRelations node = new mwHierarchyRelations();

            using (IDataContext ctx = DataContext.Instance())
            {
                try
                {
                    ctx.BeginTransaction();

                    var rep = ctx.GetRepository<mwHierarchyRelations>();
                    hierarchy = rep.Get().Where(x => x.UserID == userId);

                    if (getFullObjects)
                    {
                        foreach (var item in hierarchy)
                        {
                            var rcc = new RoleController();

                            item.User = UserController.GetUserById(portalId, userId);
                            item.Role = rcc.GetRole(item.RoleID, portalId);
                            item.Node = mwHierarchyRelationsController.GetHierarchyNode(item.HNodeID, portalId, true);
                        }
                    }
                    ctx.Commit();
                    return hierarchy;
                }
                catch (Exception)
                {
                    ctx.RollbackTransaction();
                    throw;
                }
            
            }
            
        }


        /// <summary>
        /// Gets the Hierarchy relations Text by UserID
        /// </summary>
        /// <param name="UserId">The DNN UserId</param>
        //public static List<mwHierarchyTextRelations> GetHierarchyTextRelationsByUserID(int portalId, int userId, bool getFullObjects = false)
        //{
        //    IEnumerable<mwHierarchyRelations> hierarchy;
        //    mwHierarchyRelations node = new mwHierarchyRelations();

        //    using (IDataContext ctx = DataContext.Instance())
        //    {
        //        try
        //        {
        //            ctx.BeginTransaction();

        //            var rep = ctx.GetRepository<mwHierarchyRelations>();
        //            hierarchy = rep.Get().Where(x => x.UserID == userId);

        //            var myList = new List<mwHierarchyTextRelations>();
        //            foreach (var item in hierarchy)
        //            {
        //                var myItem = new mwHierarchyTextRelations();
        //                var rcc = new RoleController();

        //                item.User = UserController.GetUserById(portalId, userId);
        //                item.Role = rcc.GetRole(item.RoleID, portalId);
        //                item.Node = mwHierarchyRelationsController.GetHierarchyNode(item.HNodeID, portalId, true);

        //                myItem.UserDisplayName = item.User.DisplayName;
        //                myItem.Username = item.User.Username;
        //                myItem.RoleName = item.Role.RoleName;
        //                myItem.NodeName = item.Node.Name;
        //                myItem.FullRoleAndRole = myItem.RoleName + " - " + myItem.NodeName;

        //                myList.Add(myItem);
        //            }

        //            ctx.Commit();
        //            return myList;
        //        }
        //        catch (Exception)
        //        {
        //            ctx.RollbackTransaction();
        //            throw;
        //        }
            
        //    }
            
        //}

        /// <summary>
        /// Add a new Node Relation
        /// </summary>
        /// <param name="node">The new Node Relation</param>
        public static void AddNodeRelation(mwHierarchyRelations node)
        {
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<mwHierarchyRelations>();
                rep.Insert(node);
            }
        }

        /// <summary>
        /// Gets the Node.
        /// </summary>
        /// <param name="nodeId">The Node id.</param>
        /// <param name="portalId">The portal id.</param>
        /// <param name="ignoreCache">if set to <c>true</c> will get Node info directly from database.</param>
        /// <returns>Node info (mwHierarchy).</returns>
        public static mwHierarchy GetHierarchyNode(int nodeId, int portalId, bool ignoreCache)
        {
            mwHierarchy node;
            using (IDataContext ctx = DataContext.Instance())
            {
                var rep = ctx.GetRepository<mwHierarchy>();
                node = rep.GetById(nodeId);
                
            }
            return node;          
        }

        ///// <summary>
        ///// Move the Node after a selected Node in the list
        ///// </summary>
        ///// <param name="node">Node to move</param>
        ///// <param name="targetNodeId">Node Target id</param>
        //public void MoveNodeAfter(mwHierarchy node, int targetNodeId)
        //{
        //    PetaPocoHelper.ExecuteNonQuery(DotNetNuke.Common.Utilities.Config.GetConnectionString(), System.Data.CommandType.StoredProcedure, "mwMoveRoleAfter", node.NodeID, targetNodeId);
        //}

        ///// <summary>
        ///// Move the Node before a selected Node in the list
        ///// </summary>
        ///// <param name="node">Node to move</param>
        ///// <param name="targetNodeId">Node Target id</param>
        //public void MoveNodeBefore(mwHierarchy node, int targetNodeId)
        //{
        //    PetaPocoHelper.ExecuteNonQuery(DotNetNuke.Common.Utilities.Config.GetConnectionString(), System.Data.CommandType.StoredProcedure, "mwMoveRoleBefore", node.NodeID, targetNodeId);
        //}

        ///// <summary>
        ///// Move the Node to a selected Node as child
        ///// </summary>
        ///// <param name="node">Node to move</param>
        ///// <param name="targetNodeId">Node Target id</param>
        //public void MoveNodeToParent(mwHierarchy node, int targetNodeId)
        //{
        //    PetaPocoHelper.ExecuteNonQuery(DotNetNuke.Common.Utilities.Config.GetConnectionString(), System.Data.CommandType.StoredProcedure, "mwMoveRoleToParent", node.NodeID, targetNodeId);
        //}
    }
}